﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace TextMIS
{
    public partial class frmResultIn : Form
    {
        public frmResultIn()
        {
            InitializeComponent();
        }

        public static string userName;
        public static string strSql;
        public static string PuserName;
        public static string itemNo;

        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
        SqlDataReader dr;

        private void frmResultIn_Load(object sender, EventArgs e)
        {
            userName = frmLogin.userName;
            conn = new
SqlConnection(frmLogin.SQLstr);
            listView1.GridLines = true;
            listView1.FullRowSelect = true;
            listView1.View = View.Details;
            listView1.Scrollable = true;
            listView1.MultiSelect = false;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            da = new SqlDataAdapter("select BranchNo,BranchName from BranchInfo ", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "BranchInfo");

            conn.Close();
            this.tscBranch.ComboBox.DataSource = ds.Tables[0];
            this.tscBranch.ComboBox.DisplayMember = "BranchName";
            this.tscBranch.ComboBox.ValueMember = "BranchNo";

            lblPname.Text = "该项目体检结果反馈到下方";
            txtResult.Enabled = false;
        }

        private void tsbOK_Click(object sender, EventArgs e)
        {
            this.ShowInfo();
        }

        private void HeaderList()
        {
            listView1.Columns.Add("注册编号", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("用户姓名", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("体检部门", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("体检项目", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("体检结果", 120, HorizontalAlignment.Left);

        }

        private void ShowInfo()
        {
            try
            {
                listView1.Clear();
                da = new SqlDataAdapter("select RegisterID, Pname,BranchName ,ItemName, Result,Username from viewPI_tab where ItemName = '" + tscItem.Text.Trim() + "'", conn);

                DataTable dt = new DataTable();
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                int count = da.Fill(dt);
                conn.Close();

                HeaderList();
                foreach (DataRow dr in dt.Rows)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.SubItems.Clear();
                    lvi.SubItems[0].Text = dr[0].ToString();
                    lvi.SubItems.Add(dr[1].ToString());
                    lvi.SubItems.Add(dr[2].ToString());
                    lvi.SubItems.Add(dr[3].ToString());
                    lvi.SubItems.Add(dr[4].ToString());
                    lvi.SubItems.Add(dr[5].ToString());
                    listView1.Items.Add(lvi);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPutIn_Click(object sender, EventArgs e)
        {
            txtResult.Text = listView1.SelectedItems[0].SubItems[4].Text;
            PuserName = listView1.SelectedItems[0].SubItems[5].Text.ToString();
            lblPname.Text = listView1.SelectedItems[0].SubItems[5].Text.ToString() + "  的体检项目  " + listView1.SelectedItems[0].SubItems[3].Text.ToString() + "  结果如下";
            txtResult.Enabled = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtResult.Clear();
        }

        public void GetItemNo()
        {
            strSql = "select * from ItemInfo  where ItemName = '" + tscItem.Text.Trim() + "'";
            com = new SqlCommand(strSql, conn);
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            dr = com.ExecuteReader();

            if (dr.Read())
            {
                itemNo = dr.GetValue(0).ToString();
            }
            conn.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            GetItemNo();
            com = new SqlCommand("update PI_tab set Result = '" +
       txtResult.Text.Trim() + "'PrePaid = '1' where Username = '" + PuserName + "'and ItemNo = '" + itemNo + "'", conn);

            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }

            com.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("成功录入并保存！");
            this.ShowInfo();
            txtResult.Enabled = false;
        }

        private void tscBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select ItemNo,ItemName from ItemInfo where BranchName='" + tscBranch.Text + "'", conn);
            DataSet ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            da.Fill(ds, "ItemInfo");

            conn.Close();

            this.tscItem.ComboBox.DataSource = ds.Tables[0];
            this.tscItem.ComboBox.DisplayMember = "ItemName";
            this.tscItem.ComboBox.ValueMember = "ItemNo";
        }



    }
}
